﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using BrainTechLLC;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace CompareMultiple.Plugin
{
    public class CompareMultipleWindowHandler : ICompareMultipleWindowHandler
    {
        public static PluginDefinitionLoaded<ICompareMultipleWindowHandler> Handler;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResizeDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResize;
        public event EventHandler<ObjectEventArgsNonRef<RotationParams, bool>> RequestRotate;

        protected CompareMultipleImagesWindow _compareWindow;
        protected CompareMultipleImagesWindow _compareWindowAsk;
        protected Window1 _parentWindow;

        public CompareMultipleImagesWindow CompareWindow { get { return _compareWindow; } }
        public CompareMultipleImagesWindow CompareWindowAsk { get { return _compareWindowAsk; } }

        public ObservableCollection<FileImageViewModel> CompareWindowFileImageViewModels
        {
            get
            {
                if (_compareWindow == null) return null;
                return _compareWindow.FileImageViewModels;
            }
        }

        public ObservableCollection<FileImageViewModel> CompareWindowAskFileImageViewModels
        {
            get
            {
                if (_compareWindowAsk == null) return null;
                return _compareWindowAsk.FileImageViewModels;
            }
        }

        public CompareMultipleWindowHandler(Window1 parentWindow, PluginDefinitionLoaded<ICompareMultipleWindowHandler> handler)
        {
            Handler = handler;
            _parentWindow = parentWindow;
        }

        public void OpenAskWindow(ObservableCollection<FileImageViewModel> items, string groupName)
        {
            if (_compareWindowAsk == null)
            {
                _compareWindowAsk = new CompareMultipleImagesWindow()
                {
                    Owner = _parentWindow,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen
                };
                _compareWindowAsk.ClosedWindow += new EventHandler<ObjectEventArgs<ObservableCollection<FileImageViewModel>>>(_compareWindowAsk_ClosedWindow);
                _compareWindowAsk.WantViewFull += new EventHandler<ObjectEventArgs<FileImageViewModel>>(_compareWindowAsk_WantViewFull);
                _compareWindowAsk.RequestDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestDelete);
                _compareWindowAsk.RequestResize += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestResize);
                _compareWindowAsk.RequestResizeDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestResizeDelete);
                _compareWindowAsk.RequestRotate += new EventHandler<ObjectEventArgsNonRef<RotationParams, bool>>(_compareWindowAsk_RequestRotate);
            }

            _compareWindowAsk.ShowActivated = true;
            _compareWindowAsk.Show();
            _compareWindowAsk.FileImageViewModels = items;
            _compareWindowAsk.compare.ShowSimilarityGroup = false;
            _compareWindowAsk.compare.btnShowInNewWindow.Visibility = Visibility.Hidden;
            _compareWindowAsk.compare.HighlightSimilar(new List<string>());
            _compareWindowAsk.WindowState = WindowState.Maximized;
            _compareWindowAsk.compare.GroupName = groupName;
        }

        void _compareWindowAsk_RequestRotate(object sender, ObjectEventArgsNonRef<RotationParams, bool> e)
        {
            RequestRotate.RaiseEvent(sender, e.obj1, e.obj2);
        }

        void _compareWindowAsk_RequestResizeDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestResizeDelete.RaiseEvent(sender, e.obj1, e.obj2);
        }

        void _compareWindowAsk_RequestResize(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestResize.RaiseEvent(sender, e.obj1, e.obj2);
        }

        void _compareWindowAsk_RequestDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestDelete.RaiseEvent(sender, e.obj1, e.obj2);
        }

        public void OpenWindow(ObservableCollection<FileImageViewModel> items)
        {
            if (_compareWindow == null)
            {
                _compareWindow = new CompareMultipleImagesWindow() { Owner = _parentWindow };
                _compareWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                _compareWindow.ClosedWindow += new EventHandler<ObjectEventArgs<ObservableCollection<FileImageViewModel>>>(_compareWindow_ClosedWindow);
                _compareWindow.WantViewFull += new EventHandler<ObjectEventArgs<FileImageViewModel>>(_compareWindow_WantViewFull);
                _compareWindow.RequestDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestDelete);
                _compareWindow.RequestResize += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestResize);
                _compareWindow.RequestResizeDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(_compareWindowAsk_RequestResizeDelete);
                _compareWindow.RequestRotate += new EventHandler<ObjectEventArgsNonRef<RotationParams, bool>>(_compareWindowAsk_RequestRotate);
                //_compareWindow.InputBindings.Add(new KeyBinding(Commands.WantCompareMultipleInNewWindow, new KeyGesture(Key.Enter)));
            }

            _compareWindow.ShowActivated = true;
            _compareWindow.Show();
            _compareWindow.FileImageViewModels = items;
            _compareWindow.WindowState = WindowState.Maximized;
        }

        private void _compareWindowAsk_WantViewFull(object sender, ObjectEventArgs<FileImageViewModel> e)
        {
            _parentWindow._FullViewerHandler.Handler.OpenWindow(e.obj);
        }

        private void _compareWindowAsk_ClosedWindow(object sender, ObjectEventArgs<ObservableCollection<FileImageViewModel>> e)
        {
            if (_compareWindowAsk != null)
            {
                _compareWindowAsk.ClosedWindow -= _compareWindowAsk_ClosedWindow;
                _compareWindowAsk.WantViewFull -= _compareWindowAsk_WantViewFull;
                _compareWindowAsk.RequestDelete -= _compareWindowAsk_RequestDelete;
                _compareWindowAsk.RequestResize -= _compareWindowAsk_RequestResize;
                _compareWindowAsk.RequestResizeDelete -= _compareWindowAsk_RequestResizeDelete;
                _compareWindowAsk.RequestRotate -= _compareWindowAsk_RequestRotate;
            }
            _compareWindowAsk = null;
        }

        private void _compareWindow_WantViewFull(object sender, ObjectEventArgs<FileImageViewModel> e)
        {
            _parentWindow._FullViewerHandler.Handler.OpenWindow(e.obj);
        }

        private void _compareWindow_ClosedWindow(object sender, ObjectEventArgs<ObservableCollection<FileImageViewModel>> e)
        {
            if (_compareWindow != null)
            {
                _compareWindow.ClosedWindow -= _compareWindow_ClosedWindow;
                _compareWindow.WantViewFull -= _compareWindow_WantViewFull;
                _compareWindow.RequestDelete -= _compareWindowAsk_RequestDelete;
                _compareWindow.RequestResize -= _compareWindowAsk_RequestResize;
                _compareWindow.RequestResizeDelete -= _compareWindowAsk_RequestResizeDelete;
                _compareWindow.RequestRotate -= _compareWindowAsk_RequestRotate;
            }
            _compareWindow = null;
        }
    }
}
